package com.pro.service.imple;

import com.pro.mapper.JsClosureMapper;
import com.pro.pojo.JsClosure;
import com.pro.pojo.JsonData;
import com.pro.service.JsClosureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.time.Instant;
import java.util.Date;
import java.util.List;

/**
 * ClassName:JsClosureServiceImple
 * Package:com.pro.service.imple
 * Description:
 *
 * @Date:2021/11/17 19:43
 * @Author: ZPT
 */
@Service
public class JsClosureServiceImple implements JsClosureService {

    @Autowired(required = false)
    private JsClosureMapper mapper;
    @Override
    public JsonData addClosure(JsClosure jsClosure) {
        jsClosure.setStartDate(new Date());
        Instant startDate = Instant.now();
        Instant instant = startDate.plus(Duration.ofDays(jsClosure.getDays()));
        Date endDate;
        try {
            endDate = new Date(instant.toEpochMilli());
            jsClosure.setEndDate(endDate);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Boolean aBoolean = mapper.addClosure(jsClosure);
        return aBoolean!=null?JsonData.buildSuccess(aBoolean):JsonData.buildError("服务端错误");
    }

    @Override
    public List<JsClosure> getClosure(String userCode,String reason,String days) {
        return mapper.getClosure(userCode,reason,days);
    }

    @Override
    public Boolean updateClosure(Integer id, Integer days) {
        return mapper.updateClosure(id,days);
    }

    @Override
    public Boolean deleteClosure(Integer id) {
        return mapper.deleteClosure(id);
    }
}
